package cs236369.hw5;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashSet;


/**
 *         
 *    ------------ THIS CLASS IS FOR DEBUG PURPOSE ONLY -------------------
 *         
 *         
 * @author user
 *
 */
public class TryThread extends Thread {

	private DBConnection dbCon;
	private WhatToDo whatToDo;
	
	public enum WhatToDo{
		ADD_CHUCK,DEL_CHUCK,ADD_CHUCK2,DEL_CHUCK2,ADD_POST,ADD_EVENT,DEL_EVENT,
		REGISTER_EVENT,ACCEPT_CONS,UNACCEPT_CONS,SEARCH
	}
	
	
	/**
	 * 
	 * @param dbCon
	 * @param whatToDo 
	 */
	public TryThread(DBConnection dbCon,WhatToDo whatToDo) {
		this.dbCon = dbCon;
		this.whatToDo = whatToDo;
	}

	public void run(){
		try {
			switch(whatToDo){
			case ADD_CHUCK:
				User chuckNurris = new User("chuckn", "123", "Chuck", "Nurris", true, new Date(0), null, null);
				dbCon.addUser(chuckNurris);
				break;
			case ADD_CHUCK2:
				User chuckNurris2 = new User("chuckn2", "123", "Chuck", "Nurris", true, new Date(0), null, null);
				dbCon.addUser(chuckNurris2);
				break;
			case DEL_CHUCK:
				dbCon.delUser("chuckn");
				break;
			case DEL_CHUCK2:
				dbCon.delUser("chuckn2");
				break;
			case ADD_POST:
				Post post = new Post("content","chuckn",new Location(-118.3079,32.9434),new Date(1234567890));
				System.out.println(dbCon.addPost(post));
				break;
			case ADD_EVENT:
				EventPost event = new EventPost("content","chuckn",new Location(-118.4079,33.9434),new Date(1234567890),8);
				event.addConsensus(new Consensus(true,"first consensus"));
				event.addConsensus(new Consensus(false,"second consensus"));
				System.out.println(dbCon.addEvent(event));
				break;
			case DEL_EVENT:
				dbCon.delEvent(6);
				break;
			case REGISTER_EVENT:
				dbCon.registerToEvent("chuckn2", 7);
				break;
			case ACCEPT_CONS:
				dbCon.acceptConsensus("chuckn2", 4, 1);
				break;
			case UNACCEPT_CONS:
				dbCon.unAcceptConsensus("chuckn", 4);
				break;
			case SEARCH:
				HashSet<Post> set = dbCon.search(new Location(-118.4100,33.9400), 450000.0, "", true, true);
				for(Post post2 : set){
					System.out.println(post2.getPostID() + ": " + post2.getUserName() + "   " 
							+ post2.getLocation().getLat() + " , " + post2.getLocation().getLong());
					if (post2 instanceof EventPost) {
						System.out.println("Consensuses:");
						EventPost event2 = (EventPost) post2;
						for(Consensus cons : event2.getConsensuses()){
							System.out.println(cons.getConsID());
							System.out.println(cons.getIsApproved());
						}
					}
					System.out.println();
				}
				break;
			}
			
		} catch (CrowdsourcingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
